﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace CPPEI.PM.Service.Model.Repository
{
    public class WorkItemCategoryRefRepository
    {
        private DbContext _dbContext;
        public WorkItemCategoryRefRepository(DbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public WorkItemCategoryRef Get(Guid workSheetId)
        {
            return _dbContext.Set<WorkItemCategoryRef>().SingleOrDefault(p => p.ParentWorkItemcategoryId == Guid.Empty && p.WorkSheetId == workSheetId);
        }

        public WorkItemCategoryRef Get(Guid workSheetId, Guid workItemCategoryId)
        {
            return _dbContext.Set<WorkItemCategoryRef>().SingleOrDefault(p => p.WorkItemCategoryId == workItemCategoryId && p.WorkSheetId == workSheetId);
        }

        public List<WorkItemCategoryRef> GetByParent(Guid workSheetId, Guid parentWorkItemCategoryId)
        {
            return _dbContext.Set<WorkItemCategoryRef>().Where(p => p.ParentWorkItemcategoryId == parentWorkItemCategoryId && p.WorkSheetId == workSheetId).ToList();
        }

        public void Create(WorkItemCategoryRef workItemCategoryRef)
        {
            _dbContext.Set<WorkItemCategoryRef>().Add(workItemCategoryRef);
        }

        public List<Guid> GetRelatedWorkSheetIdList(Guid workItemCategoryId)
        {
            return _dbContext.Set<WorkItemCategoryRef>().Where(p => p.WorkItemCategoryId == workItemCategoryId)
                .GroupBy(p => p.WorkSheetId).Select(p => p.Key).ToList();
        }
    }
}
